package bb.lanxing.device.sgdevcie.home

import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import bb.lanxing.R
import bb.lanxing.databinding.ActivityReportDeviceLogBinding
import bb.lanxing.device.sgdevcie.viewmodel.DeviceViewModel
import bb.lanxing.mvvm.base.BaseDBActivity
import bb.lanxing.util.ShareLogUtil
import kotlinx.coroutines.launch

class ReportDeviceLogActivity : BaseDBActivity<ActivityReportDeviceLogBinding>() {
    var deviceAddress: String? = ""
    var deviceName: String? = ""
    var deviceType: Int? = 17

    private val deviceViewModel by viewModels<DeviceViewModel>()


    override fun getLayoutId(): Int {
        return R.layout.activity_report_device_log
    }

    override fun initView(binding: ActivityReportDeviceLogBinding) {
        this.deviceAddress = intent?.getStringExtra("EXTRA_DEVICE_ADDRESS")
        this.deviceType = intent?.getIntExtra("EXTRA_DEVICE_TYPE", 28)
        val activity = activity
        this.deviceName = intent?.getStringExtra("EXTRA_DEVICE_NAME")
        setupActionBar(true)
        title = getString(R.string.st_report_log_title)

        lifecycleScope.launch {
            deviceViewModel.errorLogSharedFlow.collect {
                if (it.first) {
                    if (it.second.exists()) {
                        ShareLogUtil.shareFile(context, it.second, ShareLogUtil.PACKAGE_NAME_WX)
                    }
                    snack(R.string.st_no_log)
                } else {
                    snack(R.string.st_no_log)
                }
            }
        }
        binding.btnSendLog.setOnClickListener {
            deviceViewModel.deviceLog

        }
    }
}
